#encoding:utf-8

import json
import commands
import logging
import requests
import hashlib
import pipes

from PermServer.modules.RightsManage import get_acl_data_by_filepath


# CIFS继承权限标志
OBJECT_INHERIT_FLAG     = 0x01 # OI
CONTAINER_INHERIT_FLAG  = 0x02 # CI
INHERIT_ONLY_FLAG       = 0x08 # IO 
INHERITED_ACE_FLAG      = 0x10 # I

# CIFS权限类型
CIFS_ACCESS_ALLOWED = 0 # 允许
CIFS_ACCESS_DENIED  = 1 # 拒绝 

# CIFS权限
CIFS_FULL_CONTROL   =  0x001f01ff # 完全控制
CIFS_CHANGE         =  0x001301bf  # 修改
CIFS_EREAD          =  0x001200a9  
CIFS_OREAD          =  0xa01200a1
CIFS_BREAD          =  0x00120089   
CIFS_WRITE          =  0x00000116
CIFS_DELETE         =  0x00010000
CIFS_LIST           =  0x001200a9



class PermManager(object):
    """
        权限管理（获取，设置，转换）
    """
    def __init__(self, filepath=""):
        self.filepath = pipes.quote(filepath)
        self.acl_data_list = []
        self.acl_data_list = get_acl_data_by_filepath(filepath)

    def get_own(self, acl_datas):
        """
            获取文件所有者
        """
        return acl_datas[3].split(":")[1]

    def parse_acl_data(self, acl_datas):
        """
           解析对象权限信息
        """
        acl_data_list = []
        for acl_data in acl_datas:
            if "ACL" in acl_data:
                _, sid, ace = acl_data.split(":")
                items = ace.split('/')
                items.insert(0, sid)
                acl_data_list.append(items)
        return acl_data_list

    def is_inherit_ace(self, value):
        """
            判断是否是继承权限
        """
        if value & INHERITED_ACE_FLAG:
            return True
        return False




